<?php
session_start();
if(isset($_SESSION['admin_id'])) {
include "../../include/dbFunctions.php";
$id = $_SESSION['vid'];
if ($_POST['education']){
    $education = $_POST['education'];
} else {
    $education = Array(0,1,2,3,4);
}
if ($_POST['location']){
    $location = $_POST['location'];
} else {
    $location = Array(0,1,2,3,4);
}
//timeslot
$daya = Array();
$day = $_POST['day'];
if ($day =='2'){
    $daya[]=0;
    $daya[]=1;
} elseif ($day=='0'){
    $daya[]=0;
} elseif ($day=='1'){
    $daya[]=1;
}
////////////////////////////////////////dang lam do. Chan qua!!!!!!
//update education_preference table
$query2 = "DELETE FROM education_preference WHERE volunteer_id=$id";
$result2 = executeQuery($query2);
for ($i=0;$i<count($education);$i++){
    $education_id = $education[$i];
    $query3 = "INSERT INTO education_preference (volunteer_id,education_id) VALUES ($id,$education_id)";
    $result3 = executeQuery($query3);
}
//update location_preference table
$query4 = "DELETE FROM location_preference WHERE volunteer_id=$id";
$result4 = executeQuery($query4);
for ($i=0;$i<count($location);$i++){
    $location_id = $location[$i];
    $query5 = "INSERT INTO location_preference (volunteer_id,location_id) VALUES ($id,$location_id)";
    $result5 = executeQuery($query5);
}
$link = "../volunteerView.php?id=$id";
//update timeslot preference table
$query5 = "DELETE FROM time_preference WHERE volunteer_id='".$id."'";
$result5  = executeQuery($query5);
//reinsert
for ($i=0;$i<count($daya);$i++){
    $tid = $daya[$i];
    $query2 = "INSERT INTO time_preference (volunteer_id,time_id) VALUES ('".$id."','".$tid."')";
    $result2  = executeQuery($query2);
}
if (($result2!=0)||($result4!=0)){
    $_SESSION['mes']="The record has been updated successfully";
} else {
    $_SESSION['mes']="Error! Please try again";
}
header("Location: $link");
?>
   <?php
}else {
    header("Location: ../include/login.html");
}
?>